package com.java2207.group1.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java2207.group1.bo.StaffBo;
import com.java2207.group1.common.LayuiPageVo;
import com.java2207.group1.mapper.StaffMapper;
import com.java2207.group1.pojo.Staff;
import com.java2207.group1.service.StaffService;
import com.java2207.group1.vo.staff.StaffBarVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author HLJ
 * @Date 2022/7/26 07
 */
@Service("staffService")
public class StaffServiceImpl implements StaffService {

    @Autowired
    private StaffMapper staffMapper;

    @Override
    public LayuiPageVo<Staff> staffPageDate(Map<String, Object> params) {
        // 获取分页数据
        Integer page = Integer.parseInt(params.get("page") + "");
        Integer limit = Integer.parseInt(params.get("limit") + "");
        PageHelper.startPage(page, limit);

        List<Staff> list = staffMapper.selectPageDate(params);

       // mybatis 的分页数据结构
        PageInfo<Staff> staffPageInfo = new PageInfo<>(list);

        // 数据封装
        LayuiPageVo<Staff> vo = new LayuiPageVo<>();

        vo.setCode(0);
        vo.setMsg("分页列表数据");
        vo.setCount(staffPageInfo.getTotal()); // 总记录数
        vo.setData(staffPageInfo.getList());

        return vo;
    }

    @Override
    public int addStaff(Staff staff) {
        return staffMapper.addStaff(staff);
    }

    @Override
    public int deleteStaff(Integer id) {
        return staffMapper.deleteStaff(id);
    }

    @Override
    public int updateStaff(Staff staff) {
        return staffMapper.updateStaff(staff);
    }

    @Override
    public int removeStaff(String ids) {
        String[] arrId = ids.split(",");
        return staffMapper.removeStaff(arrId);
    }

    @Override
    public List<StaffBarVo> numberEchartBar() {
        return staffMapper.numberEchartBar();
    }

    @Override
    public List<StaffBo> staffExcelData(Map<String, Object> params) {
        List<Staff> staffList = staffMapper.selectPageDate(params);

        List<StaffBo> staffBos = staffList.stream().map((sta) -> {
            StaffBo staffBo = new StaffBo();
            staffBo.setId(sta.getId());
            staffBo.setEmpId(sta.getEmpId());
            staffBo.setName(sta.getName());
            staffBo.setSex(sta.getSex());
            staffBo.setAge(sta.getAge());
            staffBo.setId_no(sta.getId_no());
            staffBo.setAddress(sta.getAddress());
            staffBo.setHiredate(sta.getHiredate());
            staffBo.setTermdate(sta.getTermdate());
            staffBo.setDepartment(sta.getDepartment());
            staffBo.setTel(sta.getTel());
            staffBo.setPic(sta.getPic());
            return staffBo;
        }).collect(Collectors.toList());

        return staffBos;
    }
}
